/*
  ____        _                         _____
 |  _ \      | |                       / ____|
 | |_) | __ _| | __ _ _ __   ___ ___  | |     __ _ _ __
 |  _ < / _` | |/ _` | '_ \ / __/ _ \ | |    / _` | '__|
 | |_) | (_| | | (_| | | | | (_|  __/ | |___| (_| | |
 |____/ \__,_|_|\__,_|_| |_|\___\___|  \_____\__,_|_|
   _____           _           _   ____         __     __  _    _                 _     _
  / ____|         | |         | | |  _ \        \ \   / / | |  | |               | |   | |
 | |     ___  __ _| |_ ___  __| | | |_) |_   _   \ \_/ ___| |__| | __ _ _ __ ___ | | __| |
 | |    / _ \/ _` | __/ _ \/ _` | |  _ <| | | |   \   / _ |  __  |/ _` | '__/ _ \| |/ _` |
 | |___|  __| (_| | ||  __| (_| | | |_) | |_| |    | |  __| |  | | (_| | | | (_) | | (_| |
  \_____\___|\__,_|\__\___|\__,_| |____/ \__, |    |_|\___|_|  |_|\__,_|_|  \___/|_|\__,_|
                                          __/ |
                                         |___/

*/
#include "RGBLED.h"
// --------------------
Adafruit_NeoPixel LR_Strip = Adafruit_NeoPixel(RL_NUMS, RL_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel FB_Strip = Adafruit_NeoPixel(FB_NUMS, FB_PIN, NEO_GRB + NEO_KHZ800);

// --------------------
void RGBLED ::Initialize()
{
    FB_Strip.begin();
    LR_Strip.begin();
    FB_Strip.setBrightness(50); // 设置亮度
};

void RGBLED::extractRGB(const String &colorStr, uint8_t &r, uint8_t &g, uint8_t &b)
{
    // 移除开头的 '{' 和结尾的 '}'
    int startIndex = colorStr.indexOf('{') + 1;
    int endIndex = colorStr.lastIndexOf('}');
    String colorData = colorStr.substring(startIndex, endIndex);

    // 分割字符串并转换为整数
    int commaIndex1 = colorData.indexOf(',');
    int commaIndex2 = colorData.indexOf(',', commaIndex1 + 1);
    r = colorData.substring(0, commaIndex1).toInt();
    g = colorData.substring(commaIndex1 + 1, commaIndex2).toInt();
    b = colorData.substring(commaIndex2 + 1).toInt();
}

void RGBLED ::ResetColor(String color)
{

    uint8_t r, g, b;
    extractRGB(color, r, g, b);

    for (uint8_t i = 0; i < RL_NUMS; i++)
    {
        LR_Strip.setPixelColor(i, r, g, b);
        LR_Strip.show();
    }
    for (uint8_t k = 0; k < FB_NUMS; k++)
    {
        FB_Strip.setPixelColor(k, r, g, b);
        FB_Strip.show();
    }
};
void RGBLED ::TurnOff()
{
    for (uint8_t i = 0; i < RL_NUMS; i++)
    {
        LR_Strip.setPixelColor(i, 0, 0, 0);
        LR_Strip.show();
    }

    for (uint8_t k = 0; k < FB_NUMS; k++)
    {
        FB_Strip.setPixelColor(k, 0, 0, 0);
        FB_Strip.show();
    }
};

RGBLED rgb;